﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_ITEMDIVISION : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public M_ITEMDIVISION()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public DataTable DivisionTable { get; set; }

        void InitTable()
        {
            DataTable tbl = new DataTable();

            tbl.Columns.Add("OldDivCode", typeof(string));
            tbl.Columns.Add("DivisionCode", typeof(string));
            tbl.Columns.Add("DivisionName", typeof(string));
            tbl.Columns.Add("ITEMGROUP", typeof(string));

            this.DivisionTable = tbl;
        }
        private void M_ITEMDIVISION_Load(object sender, EventArgs e)
        {
            InitTable();

            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            this.DivisionTable = oManager.GetItemDivision();

            oManager.Close();

            gridControl1.DataSource = this.DivisionTable;
            gridControl1.Refresh();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ItemManager oManager = new ItemManager();

            try
            {
                oManager.ConnectionString = this.ConnectionString;
                System.Data.SqlClient.SqlException ex = oManager.Open();

                oManager.UpdateItemDivision(this.DivisionTable);

                oManager.Close();

                

                if (ex == null)
                {
                    this.DivisionTable.AcceptChanges();
                    MessageBox.Show("Data is successfully updated!!!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(ex.Message.ToString(), "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch(System.Data.SqlClient.SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}